var userCtrl = new XFApp.client();
XFApp.client.prototype.user = {
    isSubmitting : false,
    successCode : 10000,
    urlItems : {
        updatePwdUrl : basePath +"/user/updatePw",
        queryPageUrl : basePath + "/user/ajax/list",
        resetPwdUrl  : basePath + "/user/resetPw",
        deleteUrl    : basePath + "/user/ajax/delete"
    },
    page : null,
    updatePwdDialog : function(){
        webDialogUtil.simpleConfrimAlert('是否确认修改密码?', function(){
            userCtrl.user.updatePwd();
        });
    },
    updatePwd : function(){
        if(this.isSubmitting){
            return;
        }
        //TODO 需要增加验证
        var params = {};
        $("#update_pwd_form input").each(function(){
            var _input = $(this);
            var name = _input.attr("name");
            if(name != 'undefined' && name != '' && name != null){
                params[name] = _input.val();
            }
        });
        $.ajax({
            url: this.urlItems.updatePwdUrl,
            data: params,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                webDialogUtil.simpleAlert(data.msg);
            },
            error: function () {
                webDialogUtil.simpleAlert(type + "出现异常,请联系开发人员");
            }
        });
    },
    queryPage : function(){
        var _this = this;
        var params = {
            loginName : $("#loginName").val(),
            realName : $("#realName").val(),
            deptName : $("#deptName").val(),
            roleName : $("#roleName").val()
        }
        if(!!_this.page){
            _this.page.page("remote",0,params);
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                remote: {
                    url: _this.urlItems.queryPageUrl,
                    params: params,
                    success: function (data, pageIndex) {
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(pageResult.itemList.length>0){
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }else{
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }
                        }else{
                            webDialogUtil.simpleAlert("数据查询失败!");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询失败!");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    resetPwdDialog : function(id){
        webDialogUtil.simpleConfrimAlert("确定要重置此账号密码？", function(){userCtrl.user.resetPwd(id);});
    },
    resetPwd : function(id){
        $.ajax({
            url : this.urlItems.resetPwdUrl,
            data:{userId : id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                webDialogUtil.simpleAlert(data.msg);
            },
            error : function() {
                webDialogUtil.simpleAlert("重置密码失败，请刷新后重试!");
            }
        });
    },
    upPwdValidate : function(){
        $('#update_pwd_form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                oldPassword: {
                    group: '.col-lg-4',
                    validators: {
                        notEmpty: {message: '原密码不能为空！'}
                    }
                },
                newPassword1: {
                    group: '.col-lg-4',
                    validators: {
                        notEmpty: {message: '新密码不能为空'},
                        stringLength : {
                            min : 6,
                            max : 20,
                            message : '密码长度为6-20个字符'
                        }
                    }
                },
                newPassword2: {
                    group: '.col-lg-4',
                    validators: {
                        notEmpty: {message: '确认密码不能为空'},
                        identical: {
                            field: 'newPassword1',
                            message: '两次输入密码不一致'
                        }
                    }
                }
            }
        });
    },
    deleteUserDialog : function(userId){
        webDialogUtil.simpleConfrimAlert("确定要删除此用户？删除后不可恢复！",
            function(){userCtrl.user.deleteUser(userId);});
    },
    deleteUser : function(userId){
        var _this = this;
        $.ajax({
            url:_this.urlItems.deleteUrl,
            data:{userId : userId},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(data.code == _this.successCode){
                    webDialogUtil.callbackConfrimAlert(data.msg, function(){userCtrl.user.queryPage();});
                }else{
                    webDialogUtil.simpleAlert(data.msg);
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("删除用户失败，请刷新后重试!");
            }
        });
    },
    event : function(){
        var _this = this;
        $("#update_pwd_button").on("click",function(){
            $('#update_pwd_form').bootstrapValidator('validate');
            if($('#update_pwd_form').data('bootstrapValidator').isValid()){
                _this.updatePwdDialog();
            }
        });
        if(!!document.getElementById("page_grid")){
            _this.queryPage();
        }
        _this.upPwdValidate();
        $("#user_query").on("click", function(){
            _this.queryPage();
        });
    }
}

$(function(){
    //扩展

    userCtrl.user.event();
});